<?php
	session_start();
	$_judul = $_POST['judul'];
	$_username = $_SESSION['username'];
	if (isset($_POST['sex'])) $_pil = $_POST["sex"];
		else $_pil="";
	$_tag = $_POST['tag'];
	if (strcmp($_pil, "link")==0){
		$_artikel = $_POST['artikel'];
		$_about = $_POST['about'];
	}else if (strcmp($_pil, "gambar")==0){
		if (isset($_POST['avatar'])){
			$_ava = $_FILES["avatar"]["name"];
		}else{
			$_ava=$_FILES["avatar"]["name"];
		}
	}else if (strcmp($_pil, "video")==0){
		$_youtube = $_POST['youtube'];
		$_youtube=trim($_youtube);
	}
	require("dbfunc.php");
	
	//hapus sapsi tag
	$tag=trim($_tag);
	//tokenisasi tag
	$token = strtok($tag, ",");
	$i=0;
	while ($token != false)
	{
		$tags[$i]=trim($token);
		$token = strtok(",");
		$i++;
	}
	//filter tag duplikasi di dbase
	$j=0;
	for ($k=0; $k<count($tags); $k++){
		$qres = mysql_query("SELECT * FROM tag WHERE nama = '$tags[$k]'");
		if (!$qres) die('Error-tag: ' . mysql_error());
		if (mysql_num_rows($qres)<1){
			$ltags[$j]=$tags[$k];
			$j++;
		}
	}
	//insert ke konten
	$_tgl = date("Y-m-d H:i:s");
	//cari konten sebelumnya
	if (strcmp($_pil, "link")==0){
		$_pil=1;
		$qres="INSERT INTO konten (id, userpost, tgl, judul, tipe, url, about, tag, jmlLike, jmlKom) VALUES (NULL,'$_username','$_tgl','$_judul','$_pil','$_artikel','$_about','$tag','0','0')";
	}else if (strcmp($_pil, "gambar")==0){
		$_pil=2;
		if ($_FILES['avatar']['error'] > 0) {
			echo "Error: " . $_FILES['avatar']['error'] . "<br />";
			//header('Location: post.php');
		}else{
			move_uploaded_file($_FILES['avatar']['tmp_name'],"konten/".$_judul.".jpg");
			$qres="INSERT INTO konten (id, userpost, tgl, judul, tipe, url, about, tag, jmlLike, jmlKom) VALUES (NULL,'$_username','$_tgl','$_judul','$_pil',NULL,NULL,'$tag','0','0')";
		}
	}else if (strcmp($_pil, "video")==0){
		$_pil=3;
		$yt=substr($_youtube,31,11);
		$_youtube='http://www.youtube.com/embed/'.$yt.'?rel=0';
		$qres="INSERT INTO konten (id, userpost, tgl, judul, tipe, url, about, tag, jmlLike, jmlKom) VALUES (NULL,'$_username','$_tgl','$_judul','$_pil','$_youtube',NULL,'$tag','0','0')";
	}
	if (!mysql_query($qres,$con))
	{
		die('Error1: ' . mysql_error());
	}
	//update ke user
	$qres = mysql_query("SELECT jmlPost FROM user WHERE username='$_username'");
	$jpost=0;
	if (!$qres) die('Error-jpost: ' . mysql_error());
	while($row = mysql_fetch_array($qres))
	{
		$jpost=$row['jmlPost'];
	}
	echo "ini jpost=".$jpost."<br />";
	$jpost++;
	if ($jpost==1){
		$qres="INSERT INTO achievement (user,jenis) VALUES ('$_username','1')";
		if (!mysql_query($qres,$con))
		{
			die('Error: ' . mysql_error());
		}
	}
	if ($jpost==10){
		$qres="INSERT INTO achievement (user,jenis) VALUES ('$_username','2')";
		if (!mysql_query($qres,$con))
		{
			die('Error: ' . mysql_error());
		}
	}
	$qres="UPDATE user SET  jmlPost =  '$jpost' WHERE  username = '$_username'";
	if (!mysql_query($qres,$con))
	{
		die('Error2: ' . mysql_error());
	}
	//insert ke posting
	$qres="INSERT INTO posting (username,judulkonten) VALUES ('$_username','$_judul')";
	if (!mysql_query($qres,$con))
	{
		die('Error3: ' . mysql_error());
	}
	//insert tag
	for ($t=0; $t<count($ltags); $t++){
		$qres="INSERT INTO tag (nama) VALUES ('$ltags[$t]')";
		if (!mysql_query($qres,$con))
		{
			die('Error4: ' . mysql_error());
			die('<br />Duplicate entry, go back to <a href="post.php">POST</a>');
		}
	}
	
	echo "Selamat, ".$_username." . Anda telah berhasil posting ".$_judul;
	header("Location: konten.php?jd=$_judul");
	
	mysql_close($con);
?>